package com.itheima.service.impl;

import com.itheima.mapper.DeptMapper;
import com.itheima.pojo.Dept;
import com.itheima.service.DeptService;
import java.time.LocalDateTime;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;


@Service
public class DeptServiceImpl implements DeptService {

    @Autowired
    private DeptMapper deptMapper;

    @Override
    public List<Dept> list() {
        // 结果 mapper
        return deptMapper.selectList();
    }

    @Override
    public void removeById(Integer id) {
        deptMapper.deleteById(id);
    }

    @Override
    public void create(Dept dept) {
        // 判断这个name是不是已经存在啦？
        // 怎么判断? 1.利用实体去判断 2.用数量去判断(推荐)
        // 1. 非预期错误变成预期错误 - 在我们预期范围内
        // 2. 错误流程做了优化
        // 3. 指定的异常就可以指定的捕获 - 全部异常捕获
        Integer count = deptMapper.selectCountByName(dept.getName());
        if (count > 0) {
            throw new RuntimeException("部门已经存在了");
        }
        dept.setCreateTime(LocalDateTime.now());
        dept.setUpdateTime(LocalDateTime.now());
        deptMapper.insert(dept);
    }
}
